package com.hks360.car_treasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.internet.request_net.constant.NetActionName;
import com.hks360.car_treasure.internet.request_net.nohttp.user_interface.HttpCallback;
import com.hks360.car_treasure.internet.request_net.nohttp.user_method.CallServerManager;
import com.hks360.car_treasure.internet.request_net.nohttp.user_requstparam.BindUser;
import com.hks360.car_treasure.model.CarLocation;
import com.hks360.car_treasure.model.CarState;
import com.hks360.car_treasure.model.TboxInfo;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenter;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenterImpl;
import com.hks360.car_treasure.mvp.equipment.view.EquipmentView;
import com.hks360.car_treasure.util.GsonUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.nohttp.Response;
import com.hks360.third_party_library.util.GsonService;

import java.util.List;

/**
 * 连接设备
 */
public class ConnectEquipmentActivity extends BaseActivity implements EquipmentView, HttpCallback<String> {
    private EditText mVinEt;
    private EditText mTboxidEt;
    private TextView mBindUserTv;

    private EquipmentPresenter mEquipmentPresenter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            finish();
        }
    };

    @Override
    public void setupView() {
        mVinEt = UIUtil.findViewById(this, R.id.connect_vin_et);
        mTboxidEt = UIUtil.findViewById(this, R.id.connect_tboxid_et);
        mBindUserTv = UIUtil.findViewById(this, R.id.connect_tv);
    }

    @Override
    public void initData() {
        String tboxId = PrefUtil.getString(this, PrefKey.TBOX_ID, "");
        mTboxidEt.setText(tboxId);
        mEquipmentPresenter = new EquipmentPresenterImpl(this,this);
    }

    @Override
    public void addListener() {
        mBindUserTv.setOnClickListener(this);
    }

    @Override
    public void initView() {
        setContentView(R.layout.layout_binduser);
        initTitleBar(R.string.binduser_title);
        initDialog(R.string.loading_submit);
        setupView();
        initData();
        addListener();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.connect_tv:
                bind_user();
                break;
        }
    }

    /**
     * 绑定设备
     */
    private void bind_user() {
        if (!isInputError()) {
            CallServerManager.PostServer(R.id.connect_tv, GsonUtil.toJson(new BindUser(PrefUtil.getString(this, PrefKey.LOGIN_PHONE, ""), mVinEt.getText().toString().trim(), mTboxidEt.getText().toString().trim())),
                    NetActionName.BINDUSER, this);
        }
    }

    private boolean isInputError() {
        if (CommonUtil.isEmptyEt(mVinEt) || CommonUtil.isEmptyEt(mTboxidEt)) {
            CommonUtil.showToast(this, "请您将信息填写完整后再连接设备");
            return true;
        }
        return false;
    }

    private void binduser_success(String result) {

    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.dismiss();
    }

    @Override
    public String getMobile() {
        return PrefUtil.getString(this, PrefKey.LOGIN_PHONE, "");
    }

    @Override
    public String getVin() {
        return mVinEt.getText().toString().trim();
    }

    @Override
    public String getTboxId() {
        return mTboxidEt.getText().toString().trim();
    }

    @Override
    public void bindUserSuccess() {
        PrefUtil.putString(this,PrefKey.TBOX_ID,getTboxId());
        finish();
    }

    @Override
    public void getCurrentStateSuccess(CarState carState) {

    }

    @Override
    public void showToastMsg(String msg) {
        CommonUtil.showToast(this,msg);
    }

    @Override
    public void readCurrentLocationSuccess(CarLocation carLocation) {

    }

    @Override
    public void readCurrentLocationFailed() {

    }

    @Override
    public <E> void onSuccess(int what, Response<E> response) {
        List<TboxInfo> tboxInfo = null;
        try{
            tboxInfo = GsonService.parseJson(response.get().toString(), new TypeToken<List<TboxInfo>>() {
            }.getType());
            Log.e("RWQ", tboxInfo.toString());
            for (TboxInfo t:tboxInfo) {
                t.setClicked(false);
            }
            AppConfig.user.getTboxinfo().addAll(tboxInfo);
            Log.e("RWQ1", AppConfig.user.toString());
        }catch (Exception e){
            CommonUtil.showToast(this,"该设备已连接");
        }
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onFailed(int what) {

    }

    @Override
    public void onStart(int what) {
        mDialog.show();
    }

    @Override
    public void onFinish(int what) {
        mDialog.dismiss();
    }
}
